#include "niv3.h"
#include "main.h"

void printIPV6(const u_char* buff, Stats* stats)
{
    const struct ip6_hdr *ip6_hdr = (const struct ip6_hdr*)buff;
    int next_header;
    int len;
    char addr[42];
    struct in6_addr src;
    struct in6_addr dest;

    char temp1[100],temp2[100];
    //Adresses
    src = ip6_hdr->ip6_src;
    dest = ip6_hdr->ip6_dst;
    inet_ntop(AF_INET6,&src,addr,41);
    sprintf(temp1,"de %s ",addr);
    inet_ntop(AF_INET6,&dest,addr,41);
    sprintf(temp2,"à %s \n",addr);
    char temp[100];
    strcpy(temp,"IPv6 : ");
    strcat(temp,temp1);
    strcat(temp,temp2);
    printD(temp);
    
    
    //Prochain entete
    next_header = ip6_hdr->ip6_nxt;
    len = sizeof(struct ip6_hdr);
    switch (next_header) {
        case IPPROTO_ICMPV6 : printD("ICMPv6\n");break;
        case IPPROTO_ROUTING : printD("ROUTINGs\n");break;
        case IPPROTO_UDP : printUDP(buff+len,stats);break;
        case IPPROTO_TCP : printTCP(buff+len,stats);break;
        case IPPROTO_IGMP : printD("IGMPv6\n");break;
        case IPPROTO_VRRP : printD("VRRP\n");break;
        default : printD("*****Protocole ipv6 inconnu  ****\n");
    }
}
void printIP(const u_char* buff, Stats* stats)
{
    const struct iphdr *ip_hdr=(const struct iphdr *)(buff);
    int len_header_ip;
    int protocol_ip;
    struct in_addr src;
    struct in_addr dest;
    char addr[18];
    char temp1[50];
    char temp2[50];

    //Adresses
    src.s_addr = ip_hdr->saddr;
    dest.s_addr = ip_hdr->daddr;
    inet_ntop(AF_INET,&src,addr,17);
    sprintf(temp1,"IPv4 : De %s ",addr);
    inet_ntop(AF_INET,&dest,addr,17);
    sprintf(temp2,"à %s \n",addr);
    strcat(temp1,temp2);
    printD(temp1);
    
    //Prochain entete
    len_header_ip = (htons(ip_hdr->ihl)>>8)*4;
    protocol_ip = htons(ip_hdr->protocol)>>8;
    switch (protocol_ip) {
        case IPPROTO_ICMP : printD("ICMP :\n"); break;
        case IPPROTO_TCP : printTCP(buff+len_header_ip,stats);break;
        case IPPROTO_UDP : printUDP(buff+len_header_ip,stats);break;
        case IPPROTO_IPIP : printIP(buff+len_header_ip,stats);break;
        case IPPROTO_AH : printD("AH :\n");break;
        case IPPROTO_EGP : printD("EGP :\n");break;
        case IPPROTO_IGMP : printD("IGMP :\n");break;
        case IPPROTO_PUP : printD("PUP :\n");break;
        case IPPROTO_DCCP : printD("DCCP :\n");break;
        case IPPROTO_VRRP : printD("VRRP\n");break;
        default : printD("****Protocole IP inconnu****: \n");
    }
}

